﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;


namespace Marmat.Forms
{
    public partial class FileManager
    {

        [Browsable(false)]
        public FileCollection Files
        {
            get
            {
                if (this._files is null)
                    this._files = new FileCollection(this);
                return this._files;
            }
        }

        private FileCollection _files;

        [Bindable(true)]
        [DefaultValue(null)]
        public virtual string Value
        {
            get => this._value;
            set
            {
                if (!string.Equals(this.Value ?? "", value ?? ""))
                {
                    this._value = value;
                    if (this.Updating == false)
                    {
                        var collection = new FileCollection(this);
                        if (!string.IsNullOrEmpty(value))
                        {
                            collection.list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FileItem>>(value);
                        }
                        this._files = collection;
                    }
                    this.Invalidate();
                }
            }
        }

        private string _value = null;

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Size AutoScrollMinSize
        {
            get => base.AutoScrollMinSize;
            set => base.AutoScrollMinSize = value;
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override bool AutoScroll
        {
            get => base.AutoScroll;
            set => base.AutoScroll = value;
        }

        [DefaultValue(null)]
        public string RootDir
        {
            get => this._RootDir;
            set
            {
                if (!string.IsNullOrEmpty(value) && !value.EndsWith("/"))
                {
                    value += "/";
                }
                this._RootDir = value;
            }
        }

        private string _RootDir;

        /// <summary>
        /// 是否自动保存附件。
        /// </summary>
        /// <returns></returns>
        [DefaultValue(false)]
        public bool AutoSave
        {
            get => this.m_autoSave;
            set => this.m_autoSave = value;
        }

        private bool m_autoSave = false;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public BindingField BindingField
        {
            get
            {
                if (this._BindingField is null)
                    this._BindingField = new BindingField(this, "Value", null, null, true, DataSourceUpdateMode.OnValidation, this);
                return this._BindingField;
            }
        }

        private BindingField _BindingField;

        /// <summary>
        /// 获取或设置一个值，该值指示控件的只读属性。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(typeof(BooleanEnum), "Default")]
        [Category("行为")]
        [Description("获取或设置一个值，该值指示文本框中的文本是否为只读。")]
        public virtual BooleanEnum ReadOnly
        {
            get => this.m_ReadOnly;
            set
            {
                this.m_ReadOnly = value;
                switch (value)
                {
                    case BooleanEnum.True:
                        this.IsReadOnly = true;
                        break;
                    case BooleanEnum.False:
                        this.IsReadOnly = false;
                        break;
                    case BooleanEnum.Default:
                        if (this.BindingField.DataSource is not null && !string.IsNullOrEmpty(this.BindingField.DataMember))
                            this.IsReadOnly = !this.BindingField.DataSource.AllowEdit || this.BindingField.DataSource.GetColumnCollection().FirstOrDefault(item => string.Equals(item.Name, this.BindingField.DataMember)).ReadOnly;
                        else
                            this.IsReadOnly = false;
                        break;
                }
                this.Invalidate();
            }
        }

        private BooleanEnum m_ReadOnly = BooleanEnum.Default;

        /// <summary>
        /// 获取实际的只读状态。
        /// </summary>
        protected bool IsReadOnly { get; set; }
    }
}